import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:loading_animation_widget/loading_animation_widget.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

class Loading {
  /// 初始化加载框
  static initLoading({bool? darkMode}) {
    Color backgroundColor;
    if (darkMode ?? true) {
      backgroundColor = Colors.grey[850]!;
    } else {
      backgroundColor = Colors.black.withOpacity(0.7);
    }

    EasyLoading.instance
      ..displayDuration = const Duration(milliseconds: 2000)
      ..indicatorType = EasyLoadingIndicatorType.ring
      ..loadingStyle = EasyLoadingStyle.custom
      ..indicatorSize = 35.0
      ..lineWidth = 2
      ..radius = 10.0
      // ..progressColor = Colors.blue
      // ..backgroundColor = Colors.white // 弹窗背景色
      // ..indicatorColor = Colors.purple
      // ..textColor = Colors.red
      // ..maskColor = Colors.black.withOpacity(0.6) // 黑色半透明
      // ..maskType = EasyLoadingMaskType.custom // 自定义遮盖层颜色
      ..progressColor = Colors.white
      ..backgroundColor = backgroundColor // 弹窗背景色
      ..boxShadow = [
        BoxShadow(
            color: Colors.white.withAlpha(15), // 阴影颜色
            offset: const Offset(0, 0), // 阴影在X轴和Y轴上的偏移
            blurRadius: 2.0, // 阴影模糊程度
            spreadRadius: 0 // 阴影扩散的程度，取值可以正数，也可以是负数
            )
      ]
      ..indicatorColor = Colors.white
      ..textColor = Colors.white
      ..maskColor = Colors.black.withOpacity(0.6)
      ..maskType = EasyLoadingMaskType.none // 无遮盖层
      ..userInteractions = true
      ..dismissOnTap = false;
  }

  // 是否正在显示
  static bool get isShowing {
    return EasyLoading.isShow;
  }

  /// 显示加载框
  static Future<void> show(
    String? status, {
    bool? userInteractions,
    Duration? overtime,
    Widget? indicator,
    EasyLoadingMaskType? maskType,
    bool? dismissOnTap,
  }) {
    /// 显示时禁止交互
    EasyLoading().userInteractions = userInteractions ?? false;

    if (overtime != null) {
      /// 设置时间
      EasyLoading().displayDuration = overtime;
      EasyLoading().animationDuration = overtime;
    }
    return EasyLoading.show(
        status: status,
        indicator: indicator,
        maskType: maskType,
        dismissOnTap: dismissOnTap);
  }

  /// 显示加载框
  static Future<void> showLoading(
    String? status, {
    Duration? duration,
    bool? userInteractions = false,
    bool? dismissOnTap,
  }) {
    EasyLoading().userInteractions = userInteractions;
    EasyLoading().infoWidget = loadingWidget();
    return EasyLoading.showInfo(status ?? '',
        duration: duration ?? const Duration(seconds: 30),
        dismissOnTap: dismissOnTap);
  }

  // 显示加载框同时携带取消按钮
  static Future<void> showLoadingWithCancel(
    String content, {
    bool? userInteractions,
    Duration? overtime,
    EasyLoadingMaskType? maskType,
    String? cancelText,
    VoidCallback? onCancel,
  }) {
    /// 显示时禁止交互
    EasyLoading().userInteractions = userInteractions ?? false;

    if (overtime != null) {
      /// 设置时间
      EasyLoading().displayDuration = overtime;
      EasyLoading().animationDuration = overtime;
    }
    return EasyLoading.show(
      indicator: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          SizedBox(height: CONTENT_MARGIN),
          loadingWidget(),
          SizedBox(height: CONTENT_MARGIN),
          Text(
            content,
            style: const TextStyle(color: Colors.white),
          ),
          SizedBox(height: CONTENT_MARGIN),
          ElevatedButton(
            onPressed: () {
              dismiss();
              if (onCancel != null) {
                onCancel();
              }
            },
            style: ElevatedButton.styleFrom(
              padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 5),
              backgroundColor: Colors.white24,
              shape: RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(20),
              ),
            ),
            child: Text(
              cancelText ?? '取消',
              style: const TextStyle(color: Colors.white),
            ),
          ),
        ],
      ),
      maskType: maskType,
    );
  }

  /// 加载动画
  static Widget loadingWidget({Color? color}) {
    // 加载gif图片
    // EasyLoading().infoWidget = Container(
    //     width: 50,
    //     height: 50,
    //     child: Image.asset('assets/images/loading_small.gif'));
    return SizedBox(
        width: 35,
        height: 25,
        child: LoadingAnimationWidget.staggeredDotsWave(
            color: color ?? Colors.white, size: 25));
  }

  static Future<void> dismiss() {
    EasyLoading.instance.userInteractions = true;
    return EasyLoading.dismiss(animation: false);
  }

  static Future<void> showSuccess(
    String status, {
    Duration? duration,
    EasyLoadingMaskType? maskType,
    bool? dismissOnTap,
  }) {
    // EasyLoading.showSuccess(status,
    //     duration: duration, maskType: maskType, dismissOnTap: dismissOnTap);

    EasyLoading().userInteractions = false;
    EasyLoading().infoWidget =
        const Icon(Icons.check_rounded, size: 35, color: Colors.white);
    return EasyLoading.showInfo(status,
        duration: duration ?? EasyLoading.instance.displayDuration,
        dismissOnTap: dismissOnTap);
  }

  static Future<void> showError(
    String status, {
    Duration? duration,
    EasyLoadingMaskType? maskType,
    bool? dismissOnTap,
  }) {
    // EasyLoading.showError(status,
    //     duration: duration, maskType: maskType, dismissOnTap: dismissOnTap);
    EasyLoading().userInteractions = false;
    EasyLoading().infoWidget =
        const Icon(Icons.close_rounded, size: 35, color: Colors.white);
    return EasyLoading.showInfo(status,
        duration: duration ?? EasyLoading.instance.displayDuration,
        dismissOnTap: dismissOnTap);
  }

  static Future<void> showToast(
    String status, {
    Duration? duration,
    EasyLoadingToastPosition? toastPosition,
    EasyLoadingMaskType? maskType,
    bool? dismissOnTap,
  }) {
    return EasyLoading.showToast(status,
        duration: duration,
        toastPosition: toastPosition,
        maskType: maskType,
        dismissOnTap: dismissOnTap);
  }

  static Future<void> showProgress(
    double value, {
    String? status,
    EasyLoadingMaskType? maskType,
  }) {
    return EasyLoading.showProgress(value, status: status, maskType: maskType);
  }

  static Future<void> showCustomProgress(
    double value, {
    String? status,
    EasyLoadingMaskType? maskType,
  }) {
    /// 显示时禁止交互
    EasyLoading().infoWidget = loadingWidget();

    return EasyLoading.showProgress(value, status: status, maskType: maskType);
  }
}
